package org.poem.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.poem.entity.TTaskDetailTable;
import org.poem.id.SnowFlake;
import org.poem.mapper.TTaskDetailTableMapper;
import org.poem.service.TTaskDetailTableService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TTaskDetailTableServiceImpl extends ServiceImpl<TTaskDetailTableMapper, TTaskDetailTable> implements TTaskDetailTableService {


    @Override
    public List<String> getTableByTaskId(Long taskId) {
        QueryWrapper<TTaskDetailTable> tTaskDetailTableQueryWrapper = new QueryWrapper<>();
        tTaskDetailTableQueryWrapper.lambda().eq(TTaskDetailTable::getTaskDetailId, taskId);
        List<TTaskDetailTable> tTaskDetailTables = list(tTaskDetailTableQueryWrapper);
        return tTaskDetailTables.parallelStream().map(
                TTaskDetailTable::getTableName
        ).collect(Collectors.toList());
    }
    @Override
    public void  saveTable(List<String> table, Long taskId){
        QueryWrapper<TTaskDetailTable> tTaskDetailTableQueryWrapper = new QueryWrapper<>();
        tTaskDetailTableQueryWrapper.lambda().eq(TTaskDetailTable::getTaskDetailId, taskId);
        this.remove(tTaskDetailTableQueryWrapper);


        List<TTaskDetailTable> tTaskDetailTables = Lists.newArrayList();
        for (String s : table) {
            TTaskDetailTable tTaskDetailTable  = new TTaskDetailTable();
            tTaskDetailTable.setId(SnowFlake.genLongId());
            tTaskDetailTable.setCreateTime(LocalDateTime.now());
            tTaskDetailTable.setTableName(s);
            tTaskDetailTable.setTaskDetailId(taskId);
            tTaskDetailTable.setStatus(1);
            tTaskDetailTable.setUpdateTime(LocalDateTime.now());
            tTaskDetailTables.add(tTaskDetailTable);
        }
        if (!CollectionUtils.isEmpty(tTaskDetailTables)){
            this.saveBatch(tTaskDetailTables);
        }
    }
}
